/**
 * @copyright 2020 Indie Semiconductor.
 *
 * This file is proprietary to Indie Semiconductor.
 * All rights reserved. Reproduction or distribution, in whole
 * or in part, is forbidden except by express written permission
 * of Indie Semiconductor.
 * @file linMasterTask.c
 * @Author: Jack.Pan
 * @E-mail:jack.pan@indiemicro.com
 * @Date: 2018/05/10
 */

#include <linMasterTask.h>
#include <lin_device.h>

#if LIN_MASTER_EN == 1

static TaskState_t linmTaskState = TASK_STATE_INIT;
static LIN_Device_Frame_t activelinmFrame;
void linmSendTimerExpired(SoftTimer_t *timer);
void LIN_Master_ISR(LIN_Device_Frame_t const *frame);
void hanldeCommands(LIN_Device_Frame_t *frame);
uint8_t global_mode = 0;
uint8_t global_color = 0;
uint8_t global_dimming = 0;

static LIN_Device_Frame_t linm_cmd_table[] = {
    [0] = {
        .frame_id = 0x20U,
        .msg_type = LIN_MSG_TYPE_TX,
        .checksum = LIN_CHECKSUM_ENHANCED,
        .length   = LIN_BUFF_SIZE,
    },
    [1] = {
        .frame_id = 0x21U,
        .msg_type = LIN_MSG_TYPE_RX,
        .checksum = LIN_CHECKSUM_ENHANCED,
        .length   = LIN_BUFF_SIZE,
    },
};


void linmSendTimerExpired(SoftTimer_t *timer)
{
    static uint8_t colorIndex = 0U;
    static uint8_t index = 0U;
    linm_cmd_table[index].data[0] = 0x01U;
    linm_cmd_table[index].data[1] = 0x00U + (2U << 4U);
    linm_cmd_table[index].data[5] = 255U;
    linm_cmd_table[index].data[6] = 0U;
    
    if (colorIndex == 0U){
        linm_cmd_table[index].data[2] = 255U;
        linm_cmd_table[index].data[3] = 0U;
        linm_cmd_table[index].data[4] = 0U;
        colorIndex = 1U;
    }else if (colorIndex == 1U){
        linm_cmd_table[index].data[2] = 0U;
        linm_cmd_table[index].data[3] = 255U;
        linm_cmd_table[index].data[4] = 0U;
        colorIndex = 2U;
    }else{
        linm_cmd_table[index].data[2] = 0U;
        linm_cmd_table[index].data[3] = 0U;
        linm_cmd_table[index].data[4] = 255U;
        colorIndex = 0U;
    }
    
    (void)LINM_SendFrame(&linm_cmd_table[index]);
    index++;
    if (index >= sizeof(linm_cmd_table)/sizeof(LIN_Device_Frame_t)){
        index = 0U;
    }
}


static SoftTimer_t linmSendTimer = {
    .mode     = TIMER_PERIODIC_MODE,
    .interval = 1000U,
    .handler  = linmSendTimerExpired
};


void LIN_Master_ISR(LIN_Device_Frame_t const *frame)
{
    activelinmFrame = *frame;
    TM_PostTask(TASK_ID_LINM);
}

void hanldeCommands(LIN_Device_Frame_t *frame)
{
    /* Handle received command */
    
}

void LINM_TaskHandler(void)
{
    switch(linmTaskState){
    case TASK_STATE_ACTIVE:
      hanldeCommands(&activelinmFrame);
      break;
    case TASK_STATE_INIT:
      LINM_Init();
      LINM_RegisterLINM_IRQ(LIN_Master_ISR);
      
      SoftTimer_Start(&linmSendTimer);
      linmTaskState = TASK_STATE_ACTIVE;
      break;
    default:
      break;
    }
}

#endif
